//
//  EeAudioControl.swift
//  Aiwei
//
//  Created by zhu xietong on 2018/1/4.
//  Copyright © 2018年 zhu xietong. All rights reserved.
//

import UIKit
import Eelay



class FAudioDiskView: JoView,AudioDiskObserver{
    
    
    static let global = FAudioDiskView()
    
    var imageV = UIImageView()
    
    
    func observerDisk(url: String, degree: CGFloat, status: EeRotationStatus,complete:CGFloat) {
        corner = degree * 10.0
    }
    
    var corner:CGFloat = 0{
        didSet{
            imageV.transform = CGAffineTransform(rotationAngle: corner)
        }
    }
    
    
    var rotationBlock:(CGFloat,EeRotationStatus)->Void = {_,_ in}
    
 
    override func addLayoutRules() {
        eelay = [
            [imageV,[ee.T.L.B.R]]
        ]
    }
    

    var isFirst = true
    
    override func didMoveToWindow() {
        super.didMoveToWindow()
        if isFirst{
            weak var wself = self
            EeAudioDiskView.global.observers.append(wself)
            isFirst = false
        }
        
    }

    
    override var intrinsicContentSize: CGSize{
        return CGSize(width: 14, height: 14)
    }
}


class EeAudioFloatBar:JoView{
    static var _shared:EeAudioFloatBar? = nil

    static var shared:EeAudioFloatBar{
        get{
            guard let s = EeAudioFloatBar._shared else{
                let _s = EeAudioFloatBar()
                EeAudioFloatBar._shared = _s
                return _s
            }
            return s
        }
    }
    let radioV = FAudioDiskView.global

    //是否现实全屏播放器
    var isShowDetail:Bool = false{
        didSet{
            checkAppear()
        }
    }
    
    
    //播放状态
    var status:Jukebox.State = Jukebox.State.ready
    {
        didSet{
            checkAppear()
        }
    }
    
    
    func checkAppear() {
        if isShowDetail{
           self.isAppear = false
        }
        else{
            switch status{
            case .playing:
                self.isAppear = true
            default:
                self.isAppear = false
            }
        }
        
    }
    var isAppear:Bool = false{
        didSet{
            if isAppear{
                self.show()
            }
            else{
                self.dissmiss()
            }
        }
    }

    
    
    let originY:CGFloat = TP.topHeight + 100
    
    open var rootBack:UIView?{
        get{
            var back:UIView? = UIApplication.shared.keyWindow
            if back == nil{
                back = UIApplication.shared.keyWindow?.rootViewController?.view
            }
            return back
        }
    }
    
    let fsize:(w:CGFloat,h:CGFloat) = (70,18)
    override func addLayoutRules() {
        let bt = UIButton()
        eelay = [
            [radioV,[ee.L.Y,[2,0]],"14",14],
            [bt,[ee.T.L.B.R]]
        ]
        bt.addTarget(self, action: #selector(openAudioPlayer), for: .touchUpInside)
        self.frame =  CGRect(x: Swidth, y: originY, width: fsize.w, height: fsize.h)

        radioV.imageV.image = UIImage(named: "music_discf16", in: Bundle(for: EeAudioPlayer.self), compatibleWith: nil)
        self.bsui.background(hex: "#eeec").set(radius:9, width: TP.separatorHeight, color: UIColor(shex:"#2222"))
    }
    
    @objc func openAudioPlayer() {
        EeAudioControl.showControl()
    }
    
    override var intrinsicContentSize: CGSize{
        return CGSize(width: fsize.w, height: fsize.h)
    }
    
    
    func dissmiss() {
        if let _ = rootBack
        {
            //                cons.removeAll()
            //                weak var wself = self
            //                guard let ws = wself else{return}
            //                let lays:TP.lays = [
            //                    [ws,[ee.T,(TP.topHeight + 80.0)],60,"18",[ee.R,80]],
            //                    ]
            //                let cs = back.setEeLays(lays: lays).1
            //                self.cons = cs
            //                self.right_constrain = cs.last!
            
            
            UIView.beginAnimations(nil, context: nil)
            UIView.setAnimationDuration(0.2)
            UIView.setAnimationCurve(UIView.AnimationCurve.init(rawValue: 7)!)
            self.frame = CGRect(x: Swidth, y: originY, width: fsize.w, height: fsize.h)
            UIView.commitAnimations()
            
        }
        
    }
    
    
    func show() {
        if let _ = rootBack
        {
            if self.superview == nil{
                rootBack?.addSubview(self)
            }
            UIView.beginAnimations(nil, context: nil)
            UIView.setAnimationDuration(0.2)
            UIView.setAnimationCurve(UIView.AnimationCurve.init(rawValue: 7)!)
            self.frame = CGRect(x: (Swidth - frame.size.width + 20), y: originY, width: fsize.w, height: fsize.h)
            UIView.commitAnimations()
            
            
            
//            back.addSubview(ws)
//            let t = NSLayoutConstraint(item: ws, attribute: .top, relatedBy: .equal, toItem: back, attribute: .top, multiplier: 1 , constant: (TP.topHeight + 80))
//            right_constrain = NSLayoutConstraint(item: ws, attribute: .right, relatedBy: .equal, toItem: back, attribute: .right, multiplier: 1, constant: 0)
//
//
//
//            cons.append(t)
//            cons.append(right_constrain)
        }
        
        
    }
}

class EeAudioControl: GesSideController {
    
    static func showControl(){
        let ctr = EeAudioControl()
        __controller.present(ctr, animated: false, completion: nil)
    
//        UIApplication.shared.keyWindow?.rootViewController?.present(ctr, animated: false, completion: nil)
    }
    
    //        EeAudioFloatBar.shared.show()
    //        let ctr = EeAudioControl()
    //        self.tabBarController?.present(ctr, animated: false, completion: nil)
    
    static var __shared:EeAudioControl? = nil
      

    static var shared:EeAudioControl{
        get{
            guard let s = EeAudioControl.__shared else{
                let _s = EeAudioControl()
                EeAudioControl.__shared = _s
                return _s
            }
            return s
        }
    }
    
    
    
    
    let player = EeAudioPlayer.shared
    override func viewDidLoad() {
        self.side = .top(height: (200 + TP.tatusBarHeight))
        super.viewDidLoad()
        let closeBt = UIButton()
        _ = TP.tatusBarHeight
        jo_contentView.eelay = [
            [player,[ee.T.L.B.R]],
            [closeBt,[ee.T.L,[TP.tatusBarHeight,20]],"20",20],
        ]
        let img = UIImage(named: "audio_cancel-music", in: Bundle(for: EeAudioPlayer.self), compatibleWith: nil)
        closeBt.setImage(img, for: .normal)
        closeBt.addTarget(self, action: #selector(close), for: .touchUpInside)
        self.gestureAble = false
//        UIApplication.shared.beginReceivingRemoteControlEvents()
        // Do any additional setup after loading the view.
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        EeAudioFloatBar.shared.isShowDetail = true

    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        EeAudioFloatBar.shared.isShowDetail = false
    }
    
    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    deinit {
//        player.removeFromSuperview()
    }
    
//    override func remoteControlReceived(with event: UIEvent?) {
//
//        if event?.type == .remoteControl {
//            switch event!.subtype {
//            case .remoteControlPlay :
//                EeAudioPlayer.sharedBox.play()
//            case .remoteControlPause :
//                EeAudioPlayer.sharedBox.pause()
//            case .remoteControlNextTrack :
//                EeAudioPlayer.sharedBox.playNext()
//            case .remoteControlPreviousTrack:
//                EeAudioPlayer.sharedBox.playPrevious()
//            case .remoteControlTogglePlayPause:
//                if EeAudioPlayer.sharedBox.state == .playing {
//                    EeAudioPlayer.sharedBox.pause()
//                } else {
//                    EeAudioPlayer.sharedBox.play()
//                }
//            default:
//                break
//            }
//        }
//    }
}
